package com.keyancompany.zhongbaopingtai.activity;


import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.weeklyDetail;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import butterknife.Bind;
import butterknife.BindString;

/**
 * @author xihe
 *         周报详情
 */
public class WeeklyDetailActivity extends BaseActivity {


    @BindString(R.string.weekly_text_detail)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.team_name)
    TextView teamName;
    @Bind(R.id.author)
    TextView author;
    @Bind(R.id.create_time)
    TextView createTime;
    @Bind(R.id.week_content)
    TextView weekContent;
    @Bind(R.id.userid)
    TextView userId;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_weekly_detail;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        initData();
    }

    private void initData() {

        String weeklyId = getIntent().getStringExtra(Constants.WEEKLY_ID);
        String teamCode = getIntent().getStringExtra(Constants.TEAM_CODE);
        if (isNetvaiable()) {
            TeamAPI.showWeekly(teamCode, weeklyId, WeeklyDetailActivity.this,
                    getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base != null && base.isSuccess()) {
            weeklyDetail weeklyDetail = JSONObject.parseObject(base.data, weeklyDetail.class);

            teamName.setText("团队:\n" + weeklyDetail.getTeamCode());
            createTime.setText(weeklyDetail.getWeekly().getCreateTime());
            userId.setText("用户Id: \n" + weeklyDetail.getUserId());
            weekContent.setText(weeklyDetail.getWeekly().getContent());
            if (weeklyDetail.getIsmyself().equals("1")) {//1：是自己创建的   0：不是自己创建的
                author.setText("作者：我");
            } else {
                author.setText("作者：" + weeklyDetail.getWeekly().getUsername());
            }
        } else {
            T.showShort(mContext, base.msg);
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }
}
